package com.pig4cloud.pig.codegen.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.net.URL;
import java.util.Scanner;

/**
 * 模块生成
 *
 * @author zsq
 * @date 2023-12-16 16:00:20
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/module")
@Slf4j
public class ModuleGenerateController {
	final static String tplModuleName="environment";
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		//使用Scanner对象
		System.out.println("请输入模块名称(只能输入字母)：");
		String newModuleName = scanner.next();//表示在控制台输入字符
		newModuleName = newModuleName.trim();
		if(!Validator.isWord(newModuleName)){
			System.out.println("只能输入英文字母");
			return;
		}
		log.info("根目录：{}",getRootPath());
		if(FileUtil.isDirectory(getRootPath()+"\\"+newModuleName)){
			System.out.println("该模块已存在:"+newModuleName);
			return;
		}
		createModule(newModuleName);
	}
	public static String getRootPath(){
		ClassLoader classLoader = ModuleGenerateController.class.getClassLoader();
		URL resource1 = classLoader.getResource("");
		return  resource1.getPath().substring(0,resource1.getPath().indexOf("pig-codegen"));
	}

	public static void createModule(String newModuleName){

		String rootPath=getRootPath();
		log.info("rootPath{}",rootPath);
		newModuleName = newModuleName.toLowerCase();
		if(FileUtil.isDirectory(rootPath+"\\"+newModuleName)){
			log.info("该模块已存在{}",newModuleName);
			return;
		}

		String moduleClassName= StrUtil.upperFirst(newModuleName);

		String generateTmpPath = rootPath+"/generate";


		String sourceFilePath = generateTmpPath+"/tpl/"+tplModuleName;
		// 目标文件路径
		String targetFilePath = generateTmpPath+"/tmp/";
		String newModule=""+newModuleName;
		String newModulePathTmp=targetFilePath+"/"+newModule;

		if(!FileUtil.isDirectory(newModulePathTmp)){
			// 复制目录（包括子目录和文件）
			FileUtil.copy(sourceFilePath, targetFilePath, false);
		}

		if(FileUtil.isDirectory(targetFilePath+"/"+tplModuleName) && !FileUtil.isDirectory(newModulePathTmp)) {
			File srcFile = new File(targetFilePath + "/"+tplModuleName);
			File targetFile = new File(newModulePathTmp);
			FileUtil.move(srcFile, targetFile, false);
		}
		//修改api目录
		if(!FileUtil.isDirectory(newModulePathTmp+"/"+newModule+"-api")){
			File apiSrcFile= new File(newModulePathTmp+"/"+tplModuleName+"-api");
			File apiTargetFile= new File(newModulePathTmp+"/"+newModule+"-api");
			FileUtil.move(apiSrcFile,apiTargetFile,false);
		}
		//修改biz目录
		if(!FileUtil.isDirectory(newModulePathTmp+"/"+newModule+"-biz")){
			File apiSrcFile= new File(newModulePathTmp+"/"+tplModuleName+"-biz");
			File apiTargetFile= new File(newModulePathTmp+"/"+newModule+"-biz");
			FileUtil.move(apiSrcFile,apiTargetFile,false);
		}

		//api模块目录
		String apiModulePath=newModulePathTmp+"/"+newModule+"-api"+"\\src\\main\\java\\com\\pig4cloud\\pig";
		if(!FileUtil.isDirectory(apiModulePath+"\\"+newModuleName)){
			File apiModuleSrcFile= new File(apiModulePath+"\\"+tplModuleName);
			File apiModuleTargetFile= new File(apiModulePath+"\\"+newModuleName);
			FileUtil.move(apiModuleSrcFile,apiModuleTargetFile,false);
		}
		//biz模块目录修改
		String bizModulePath=newModulePathTmp+"/"+newModule+"-biz"+"\\src\\main\\java\\com\\pig4cloud\\pig";
		if(!FileUtil.isDirectory(bizModulePath+"\\"+newModuleName)){
			File bizModuleSrcFile= new File(bizModulePath+"\\"+tplModuleName);
			File bizModuleTargetFile= new File(bizModulePath+"\\"+newModuleName);
			FileUtil.move(bizModuleSrcFile,bizModuleTargetFile,false);
		}

		//修改biz目录
		if(!FileUtil.isDirectory(newModulePathTmp+"/"+newModule+"-biz")){
			File bizSrcFile= new File(newModulePathTmp+"/"+tplModuleName+"-biz");
			File bizTargetFile= new File(newModulePathTmp+"/"+newModule+"-biz");
			FileUtil.move(bizSrcFile,bizTargetFile,false);
		}
		//修改biz application文件
		String applicationPath=newModulePathTmp+"/"+newModule+"-biz\\src\\main\\java\\com\\pig4cloud\\pig\\"+newModule;
		if(FileUtil.isFile(applicationPath+"\\"+StrUtil.upperFirst(tplModuleName)+"Application.java")){
			File bizSrcFile= new File(applicationPath+"\\"+StrUtil.upperFirst(tplModuleName)+"Application.java");
			File bizTargetFile= new File(applicationPath+"\\"+moduleClassName+"Application.java");
			FileUtil.move(bizSrcFile,bizTargetFile,false);
		}

//		//模块pom内容替换
		replacePom(newModulePathTmp+"/pom.xml",newModule,"模块功能描述");
		replacePom(newModulePathTmp+"/"+newModule+"-api"+"/pom.xml",newModule," API，暴露给其它模块调用");
		replacePom(newModulePathTmp+"/"+newModule+"-biz"+"/pom.xml",newModule,"，主要实现基础功能");
		//替换dockerfile 内容
		replaceFileContent(newModulePathTmp+"/"+newModule+"-biz"+"/Dockerfile",newModule);
		//替换模块名
		replaceFileContent(newModulePathTmp+"/"+newModule+"-api"+"\\src\\main\\java\\com\\pig4cloud\\pig\\"+newModule+"\\feign\\package-info.java",newModule);
		replaceFileContent(newModulePathTmp+"/"+newModule+"-biz"+"\\src\\main\\resources\\application.yml",newModule);
		replaceFileContent(newModulePathTmp+"/"+newModule+"-biz"+"\\src\\main\\java\\com\\pig4cloud\\pig\\"+newModule+"\\controller\\package-info.java",newModule);
		replaceFileContent(newModulePathTmp+"/"+newModule+"-biz"+"\\src\\main\\java\\com\\pig4cloud\\pig\\"+newModule+"\\entity\\package-info.java",newModule);
		replaceFileContent(newModulePathTmp+"/"+newModule+"-biz"+"\\src\\main\\java\\com\\pig4cloud\\pig\\"+newModule+"\\mapper\\package-info.java",newModule);
		replaceFileContent(newModulePathTmp+"/"+newModule+"-biz"+"\\src\\main\\java\\com\\pig4cloud\\pig\\"+newModule+"\\service\\package-info.java",newModule);
		replaceFileContent(applicationPath+"\\"+moduleClassName+"Application.java",moduleClassName,StrUtil.upperFirst(tplModuleName));
		replaceFileContent(applicationPath+"\\"+moduleClassName+"Application.java",newModule);

//
//		//修改主pom
		String rootPomContent = FileUtil.readString(rootPath+"pom.xml", "UTF-8");
		String backDir=rootPath+"\\generate\\bak\\"+newModuleName+"_pom";
		if(!FileUtil.isDirectory(backDir)){
			FileUtil.mkdir(backDir);
		}
		FileUtil.writeUtf8String(rootPomContent,backDir+"\\root_pom.xml");
		String rootPomContentNew=rootPomContent.replaceAll("<!-- 新模块占位符 -->","<!-- "+newModuleName+"模块 -->\n\t\t<module>"+newModuleName+"</module>\n\t\t<!-- 新模块占位符 -->");


		if(FileUtil.isFile(backDir+"\\root_pom.xml")){
			FileUtil.copy(newModulePathTmp,rootPath,false);
		}
		if(FileUtil.isDirectory(rootPath+"\\"+newModuleName)){
			if(!StrUtil.containsAny(rootPomContent,""+newModuleName)){
				FileUtil.writeUtf8String(rootPomContentNew,rootPath+"\\pom.xml");
			}
		}
		if(StrUtil.containsAny(FileUtil.readString(rootPath+"pom.xml", "UTF-8"),""+newModuleName)){
			log.info("生成模块成功：{}",newModuleName);
		}

	}


	public static void replacePom(String pomPath,String module, String description){
		String moduleMainPomContent = FileUtil.readString(pomPath, "UTF-8");
		String moduleMainPomContentNew=moduleMainPomContent.replaceAll(tplModuleName,module);
		//moduleMainPomContentNew=moduleMainPomContentNew.replaceAll("##description##",description);
		//log.info("moduleMainPomContent{}",moduleMainPomContentNew);
		FileUtil.writeUtf8String(moduleMainPomContentNew,pomPath);
	}
	public static void replaceFileContent(String filePath,String module){
		String content = FileUtil.readString(filePath, "UTF-8");
		String contentNew=content.replaceAll(tplModuleName,module);
		FileUtil.writeUtf8String(contentNew,filePath);
	}
	public static void replaceFileContent(String filePath,String module,String replace){
		String content = FileUtil.readString(filePath, "UTF-8");
		String contentNew=content.replaceAll(replace,module);
		FileUtil.writeUtf8String(contentNew,filePath);
	}


}
